import pandas as pd
def TsAbs(target1_kon,target1_koff,target2_kon,target2_koff,target3_kon,target3_koff,tumor_rate,macrophage_rate,abs):
    volumn = 2e-4 # L
    R = 6.023e+23
    # target2_kon = 0.5076 # /nm/h
    # target2_koff = 1.27e-06 #/nm

    macrophage = 1e+4 * macrophage_rate# 
    target1_expression = 50e+4
    target1 = macrophage * target1_expression/R *volumn # Mol / L

    tumor = 1e+4 * tumor_rate# 
    target2_expression = 10e+4
    target2 = tumor * target2_expression/R *volumn # Mol / L

    target3_expression = 10e+4
    target3 = tumor * target3_expression/R *volumn # Mol / L
    dimer1 = 0
    dimer2 = 0
    dimer3 = 0
    trimer12 = 0
    trimer13 = 0
    trimer23 = 0
    tetramer = 0
    interval = 0.001
    #
    metric = {}
    metric['times'] = [0]
    metric['abs'] = [abs]
    metric['target1'] = [target1]
    metric['target2'] = [target2]
    metric['target3'] = [target3]
    metric['dimer1'] = [dimer1]
    metric['dimer2'] = [dimer2]
    metric['dimer3'] = [dimer3]
    metric['trimer12'] = [trimer12]
    metric['trimer13'] = [trimer13]
    metric['trimer23'] = [trimer23]
    metric['tetramer'] = [tetramer]
    for i in np.arange(interval,72,interval): # 0到72小时变化
        # 计算梯度
        delta_abs = interval * (target1_koff *dimer1 + target2_koff * dimer2 + target3_koff * dimer3 - target1_kon * abs * target1 - target2_kon * abs * target2 - target3_kon * abs * target3)
        delta_target1 = interval * (target1_koff * (dimer1 + trimer12 + trimer13 + tetramer)  - target1_kon * (abs + dimer2 + dimer3 + trimer23) * target1)
        delta_target2 = interval * (target2_koff *(dimer2 + trimer12 + trimer23 + tetramer)  - target2_kon * (abs + dimer1 + dimer2 + trimer13) * target2)
        delta_target3 = interval * (target3_koff *(dimer3 + trimer13 + trimer23 +tetramer )  - target3_kon * (abs + dimer1 + dimer2 + trimer12) * target3)
        delta_dimer1 = interval * (target1_kon * abs * target1 - target1_koff *dimer1 +  target2_koff *trimer12 - target2_kon * dimer1 *target2+ target3_koff *trimer13 - target3_kon * dimer1 *target3)
        delta_dimer2 = interval * (target2_kon * abs * target2 - target2_koff *dimer2 +  target1_koff *trimer12 - target1_kon * dimer2 * target1+ target3_koff *trimer23 - target3_kon * dimer2 *target3)
        delta_dimer3 = interval * (target3_kon * abs * target3 - target3_koff *dimer3 +  target1_koff *trimer13 - target1_kon * dimer3 *target1+ target1_koff *trimer13 - target1_kon * dimer3 *target1)
        delta_trimer12 = interval * (target1_kon * dimer2* target1 - target1_koff*trimer12 + target2_kon * dimer1 * target2 -target2_koff * trimer12 + target3_koff * tetramer - target3_kon * trimer12 * target3)
        delta_trimer13 = interval * (target1_kon * dimer3* target1 - target1_koff*trimer13 + target3_kon * dimer1 * target3 -target3_koff * trimer13 + target2_koff * tetramer - target2_kon * trimer13 * target2)
        delta_trimer23 = interval * (target2_kon * dimer3* target2 - target2_koff*trimer23 + target3_kon * dimer2 * target3 -target3_koff * trimer23 + target1_koff * tetramer - target1_kon * trimer23 * target1)
        delta_tetramer = interval * (target3_kon * trimer12 * target3 + target2_kon * trimer13 * target2 + target1_kon * trimer23 * target1 - (target1_koff + target2_koff +target3_koff)*tetramer)
        # 更新梯度
        abs += delta_abs
        target1 += delta_target1
        target2 += delta_target2
        target3 += delta_target3
        dimer1 += delta_dimer1
        dimer2 += delta_dimer2
        dimer3 += delta_dimer3
        trimer12 += delta_trimer12
        trimer13 += delta_trimer13
        trimer23 += delta_trimer23
        tetramer += delta_tetramer
        if not (abs>=0 and target1>=0 and  target2>=0 and dimer1>=0 and dimer2>=0 and dimer3>=0 and trimer12>=0 and trimer13>=0 and trimer23>=0 and tetramer>=0):
            break
        # 保存结果
        metric['abs'].append(abs)
        metric['target1'].append(target1)
        metric['target2'].append(target2)
        metric['target3'].append(target3)
        metric['dimer1'].append(dimer1)
        metric['dimer2'].append(dimer2)
        metric['dimer3'].append(dimer3)
        metric['trimer12'].append(trimer12)
        metric['trimer13'].append(trimer13)
        metric['trimer23'].append(trimer23)
        metric['tetramer'].append(tetramer)
        metric['times'].append(i)
    metric = pd.DataFrame.from_dict(metric)
    metric = metric.set_index('times')
    # metric.to_excel(save_result,index=None)
    return metric